package com.eling.elcms.travel.dao.hibernate;

import com.eling.elcms.core.dao.hibernate.GenericDaoHibernate;
import com.eling.elcms.travel.dao.IMailDao;
import com.eling.elcms.travel.dao.ITravelContractDao;
import com.eling.elcms.travel.model.Mail;
import com.eling.elcms.travel.model.TravelContract;
import com.eling.elcms.travel.model.view.MailRegisterView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
@Repository
public class MailDaoHibernate extends 
        GenericDaoHibernate<Mail, Long> implements IMailDao{
	
	@Autowired
	private ITravelContractDao travelContractDao;
	
    public MailDaoHibernate(){
    	super(Mail.class);
    }

	@Override
	public List<MailRegisterView> queryAll(Mail cond,
			TravelContract travelContract, Boolean flag) {
		List<MailRegisterView> mrvAll = new ArrayList<MailRegisterView>();
		//全部签约会员
		List<TravelContract> list = travelContractDao.query(travelContract);
		//当前计划下所有发送邮件的会员
		List<Mail> mailList = query(cond);
		//查询全部
		if(flag == null){
			for(TravelContract contract : list){
				boolean flagC = true;//为true 则未邮寄  false 已邮寄
				Mail mailSend = new Mail();
				for(Mail mail : mailList){
					if(contract.getMember().getPkMember().equals(mail.getMember().getPkMember())){
						flagC = false;
						mailSend = mail;
					}
				}
				if(flagC){
					MailRegisterView mailRegisterView = new MailRegisterView();
					mailRegisterView.setMember(contract.getMember());
					mailRegisterView.setStatus("否");
					mrvAll.add(mailRegisterView);
				}else{
					MailRegisterView mailRegisterView = new MailRegisterView();
					mailRegisterView.setMember(mailSend.getMember());
					mailRegisterView.setCommonUser(mailSend.getCommonUser());
					mailRegisterView.setMailMethod(mailSend.getMailMethod());
					mailRegisterView.setMailTime(mailSend.getMailTime());
					mailRegisterView.setPkMail(mailRegisterView.getPkMail());
					mailRegisterView.setStatus("是");
					mrvAll.add(mailRegisterView);
				}
			}
		}else if (flag){
			//查询已邮寄
			for(Mail mail : mailList){
				MailRegisterView mailRegisterView = new MailRegisterView();
				mailRegisterView.setMember(mail.getMember());
				mailRegisterView.setCommonUser(mail.getCommonUser());
				mailRegisterView.setMailMethod(mail.getMailMethod());
				mailRegisterView.setMailTime(mail.getMailTime());
				mailRegisterView.setPkMail(mailRegisterView.getPkMail());
				mailRegisterView.setStatus("是");
				mrvAll.add(mailRegisterView);
			}
		}else{
			//查询未邮寄
			for(TravelContract contract : list){
				boolean flagC = true; //为true 则未邮寄  false 已邮寄
				for(Mail mail : mailList){
					if(contract.getMember().getPkMember().equals(mail.getMember().getPkMember())){
						flagC = false;
					}
				}
				if(flagC){ 
					MailRegisterView mailRegisterView = new MailRegisterView();
					mailRegisterView.setMember(contract.getMember());
					mailRegisterView.setStatus("否");
					mrvAll.add(mailRegisterView);
				}
			}
		}
		return mrvAll;
	}
}
